package com.springboot.sakila.controller;

import com.springboot.sakila.common.CommonResult;
import com.springboot.sakila.entity.Address;
import com.springboot.sakila.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/*
 *DATE:2025/7/14
 */
@RestController
@RequestMapping("/address")
@Slf4j
public class AddressController {
    @Resource
    private AddressService addressService;
    @GetMapping("/{id}")
    public Address findById(@PathVariable Integer id) {
        return addressService.findById(id);
    }
    @PostMapping
    public CommonResult addOne(@RequestBody Address address){
        log.info("addOne: {}", address);
        addressService.addOne(address);
        return new CommonResult(200, "success", null);
    }
    @PutMapping
    public CommonResult modOne(@RequestBody Address address){
        log.info("modOne: {}", address);
        addressService.modOne(address);
        return new CommonResult(200, "success", null);
    }
    @DeleteMapping("/{id}")
    public CommonResult deleteById(@PathVariable Integer id){
        log.info("deleteById: {}", id);
        addressService.deleteById(id);
        return new CommonResult(200, "success", null);
    }
    @GetMapping("/page")
    public CommonResult findPage(@RequestParam(required = false) String address,
                         @RequestParam(required = false) String phone,
                         @RequestParam(required = false, defaultValue = "2") Integer current,
                         @RequestParam(required = false, defaultValue = "10") Integer size,
                         @RequestParam(required = false,defaultValue = "address_id") String orderBy,
                         @RequestParam(required = false,defaultValue = "true") boolean asc) {
        log.info("findPage: {}, {}, {}, {}, {}, {}, {}", address, phone, current, size, orderBy, asc);
        log.info("排序字段：{}，升序：{}", orderBy, asc);
        return new CommonResult(200, "success",addressService.findPage(address, phone, current, size, orderBy, asc));
    }

    @GetMapping("/pageAndCity")
    public CommonResult findAAndCAndtForPages(@RequestParam(required = false) String address,
                                             @RequestParam(required = false) String phone,
                                             @RequestParam(required = false, defaultValue = "2") Integer current,
                                             @RequestParam(required = false, defaultValue = "10") Integer size,
                                             @RequestParam(required = false,defaultValue = "address_id") String orderBy,
                                             @RequestParam(required = false,defaultValue = "true") boolean asc) {
        log.info("findPage: {}, {}, {}, {}, {}, {}, {}", address, phone, current, size, orderBy, asc);
        return new CommonResult(200, "success",addressService.findAAndCAndtForPages(address, phone, current, size, orderBy, asc));
    }
}
